package com.lulin.mall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.lulin.common.utils.Query;
import com.lulin.mall.product.entity.AttrEntity;
import com.lulin.mall.product.entity.CategoryEntity;
import com.lulin.mall.product.service.AttrAttrgroupRelationService;
import com.lulin.mall.product.service.AttrService;
import com.lulin.mall.product.service.CategoryService;
import com.lulin.mall.product.vo.AttrGroupRelationVO;
import com.lulin.mall.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.lulin.mall.product.entity.AttrGroupEntity;
import com.lulin.mall.product.service.AttrGroupService;
import com.lulin.common.utils.PageUtils;
import com.lulin.common.utils.R;
import sun.rmi.runtime.Log;


/**
 * 属性分组
 *
 * @author lulin
 * @email 2032327049@qq.com
 * @date 2022-09-21 14:25:13
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    //注入属性业务层接口对象
    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationService relationService;

    /**
     * 根据三级类目的id，获取对应的属性组，以及相对应的属性信息
     *
     * @param catelogId 类目ids
     * @return
     */
    @GetMapping("/{catelogId}/withattr")
    public R getAttrgroupWithAttrs(@PathVariable("catelogId") Long catelogId) {
        //根据三级类目的id，获取对应的属性组，以及相对应的属性信息
        List<AttrGroupWithAttrsVo> list = attrGroupService.getAttrgroupWithAttrs(catelogId);

        return R.ok().put("data", list);
    }

    /**
     * 批量保存 属性组新增关联属性
     * http://localhost:8090/app/product/attrgroup/attr/relation
     *
     * @param vos
     * @returns
     */
    @PostMapping("/attr/relation")
    public R saveBatchs(@RequestBody List<AttrGroupRelationVO> vos) {
        relationService.saveBatcs(vos);
        return R.ok();
    }


    /**
     * 属性组——关联属性--查询s
     *
     * @param attrgroupId 前端传来的数组id数组
     * @return
     */
    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId) {
        //根据属性组id,找到属性id，再找到属性的信息
        List<AttrEntity> list = attrService.getRelationAttr(attrgroupId);
        return R.ok().put("data", list);
    }

    /**
     * 移除关联方法
     *
     * @param vos 属性组关联属性id的数组对象
     * @return
     */
    @PostMapping("/attr/relation/delete")
    public R relationDelete(@RequestBody AttrGroupRelationVO[] vos) {
        attrService.deleteRelation(vos);
        return R.ok();
    }

    /**
     * 查询未被关联的属性
     *
     * @param attrgroupId 前端传来的数组id数组
     * @param params
     * @return
     */
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R attrBoRelation(@PathVariable("attrgroupId") Long attrgroupId, @RequestParam Map<String, Object> params) {
        PageUtils pageUtils = attrService.getNoAttrRelation(params, attrgroupId);
        return R.ok().put("page", pageUtils);
    }

    /**
     * s
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);

        //根据找到的属性组对应的三级分类id，然后找到对应的【一级、二级、三级】数据
        Long catelogId = attrGroup.getCatelogId();
        Long[] paths = categoryService.findCateLogPath(catelogId);

        //查到后，进行封装
        attrGroup.setCatelogPath(paths);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 分页查询：根据列表编号id来查询
     * 前端提交请求，需要封装对应的分页数据
     * {
     * page:1, // 当前页
     * limit:10, // 每页显示的条数
     * sidx:"id", // 排序的字段
     * order:"asc/desc", // 排序的方式
     * key:"小米" // 查询的关键字
     * }
     */
    @RequestMapping("/list/{categoryId}")
    public R listByCategoryId(@RequestParam Map<String, Object> params, @PathVariable("categoryId") Long categoryId) {
        //自带的分页查询，,现在根据categoryId查询，因此，要重新该该方法
        //PageUtils page = attrGroupService.queryPage(params);

        //根据列表编号查询
        PageUtils page = attrGroupService.queryPage(params, categoryId);

        return R.ok().put("page", page);
    }


    /**
     * 用于获取属性分组列表数据，支持分页查询
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        // 调用服务层方法进行分页查询
        PageUtils page = attrGroupService.queryPage(params);
        // 返回查询结果，封装成R对象并包含分页数据
        return R.ok().put("page", page);
    }


    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
